/**
	This file is part of Fighting Field.

	Fighting Field is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Fighting Field is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Fighting Field.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ProgressBar.h"

#include "OgreTextAreaOverlayElement.h"

namespace FF
{
	ProgressBar::ProgressBar(const Ogre::String& name, const Style& style, const Ogre::DisplayString& caption, Ogre::Real width, Ogre::Real commentBoxWidth)
		: mProgress(0.0f), mStyle(style)
	{
		mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
			(mStyle.templateName, "BorderPanel", name);
		mElement->setWidth(width);
		Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
		mTextArea = (Ogre::TextAreaOverlayElement*)c->getChild(GetName() + "/ProgressCaption");
		Ogre::OverlayContainer* commentBox = (Ogre::OverlayContainer*)c->getChild(GetName() + "/ProgressCommentBox");
		commentBox->setWidth(commentBoxWidth);
		commentBox->setLeft(-(commentBoxWidth + 5));
		mCommentTextArea = (Ogre::TextAreaOverlayElement*)commentBox->getChild(commentBox->getName() + "/ProgressCommentText");
		mMeter = c->getChild(GetName() + "/ProgressMeter");
		mMeter->setWidth(width - 10);
		mFill = ((Ogre::OverlayContainer*)mMeter)->getChild(mMeter->getName() + "/ProgressFill");
		SetCaption(caption);
	}

	void ProgressBar::SetProgress(Ogre::Real progress)
	{
		mProgress = Ogre::Math::Clamp<Ogre::Real>(progress, 0, 1);
		mFill->setWidth((Ogre::Real)std::max<int>((int)mFill->getHeight(), (int)(mProgress * (mMeter->getWidth() - 2 * mFill->getLeft()))));
	}

	Ogre::Real ProgressBar::GetProgress()
	{
		return mProgress;
	}

	const Ogre::DisplayString& ProgressBar::GetCaption()
	{
		return mTextArea->getCaption();
	}

	void ProgressBar::SetCaption(const Ogre::DisplayString& caption)
	{
		mTextArea->setCaption(caption);
	}

	const Ogre::DisplayString& ProgressBar::GetComment()
	{
		return mCommentTextArea->getCaption();
	}

	void ProgressBar::SetComment(const Ogre::DisplayString& comment)
	{
		mCommentTextArea->setCaption(comment);
	}
}
